﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace MvcControlsToolkit
{
    public class Axis: BaseObject
    {
        /// <summary>
        /// Wether to display the axis on the graph
        /// </summary>
        public bool Show
        {
            get
            {
                if (Attributes.ContainsKey("show"))
                    return Boolean.Parse(GetString("show"));
                else
                    return true;
            }

            set
            {
                Attributes["show"] = value.ToString().ToLower();
            }
        }

        /// <summary>
        /// Wether to display the axis on the graph
        /// </summary>
        public bool ShowLabel
        {
            get
            {
                if (Attributes.ContainsKey("showLabel"))
                    return Boolean.Parse(GetString("showLabel"));
                else
                    return true;
            }

            set
            {
                Attributes["showLabel"] = value.ToString().ToLower();
            }
        }

        /// <summary>
        /// Minimum value of the axis (in data units, not pixels)
        /// </summary>
        public int Min
        {
            get
            {
                if (Attributes.ContainsKey("min"))
                    return int.Parse(Attributes["min"]);
                else return 1;
            }
            set { Attributes["min"] = value.ToString().ToLower(); }
        }

        /// <summary>
        /// Maximum value of the axis (in data units, not pixels)
        /// </summary>
        public int Max
        {
            get
            {
                if (Attributes.ContainsKey("max"))
                    return int.Parse(Attributes["max"]);
                else return 1;
            }
            set { Attributes["max"] = value.ToString().ToLower(); }
        }

        /// <summary>
        /// Padding to extend the range above and below the data bounds
        /// </summary>
        public int Pad
        {
            get
            {
                if (Attributes.ContainsKey("pad"))
                    return int.Parse(Attributes["pad"]);
                else return 1;
            }
            set { Attributes["pad"] = value.ToString().ToLower(); }
        }

        /// <summary>
        /// Padding to extend the range above data bounds
        /// </summary>
        public int PadMax
        {
            get
            {
                if (Attributes.ContainsKey("padMax"))
                    return int.Parse(Attributes["padMax"]);
                else return 1;
            }
            set { Attributes["padMax"] = value.ToString().ToLower(); }
        }

        /// <summary>
        /// Padding to extend the range above data bounds
        /// </summary>
        public int PadMin
        {
            get
            {
                if (Attributes.ContainsKey("padMin"))
                    return int.Parse(Attributes["padMin"]);
                else return 1;
            }
            set { Attributes["padMin"] = value.ToString().ToLower(); }
        }

        /// <summary>
        /// Array of ticks for the axis
        /// </summary>
        public object[] Ticks 
        {
            get
            {
                if (Attributes.ContainsKey("ticks"))
                {
                    var json = new JavaScriptSerializer();
                    var result = json.Deserialize<object[]>(Attributes["ticks"]);
                    return result;
                }
                else return null;
            }
            set
            {
                var json = new JavaScriptSerializer();
                var result = json.Serialize(value);
                Attributes["ticks"] = result.ToLower();
            }
        }

        /// <summary>
        /// Desired number of ticks
        /// </summary>
        public int NumberTicks
        {
            get
            {
                if (Attributes.ContainsKey("numberTicks"))
                    return int.Parse(Attributes["numberTicks"]);
                else return 1;
            }
            set { Attributes["numberTicks"] = value.ToString().ToLower(); }
        }

        /// <summary>
        /// Number of units between ticks
        /// </summary>
        public int TickInterval
        {
            get
            {
                if (Attributes.ContainsKey("tickInterval"))
                    return int.Parse(Attributes["tickInterval"]);
                else return 1;
            }
            set { Attributes["tickInterval"] = value.ToString().ToLower(); }
        }

        /// <summary>
        /// Wether to show the ticks (both marks and labels) or not
        /// </summary>
        public bool IsShowTicks
        {
            get
            {
                if (Attributes.ContainsKey("showTicks"))
                    return Boolean.Parse(GetString("showTicks"));
                else
                    return true;
            }

            set
            {
                Attributes["showTicks"] = value.ToString().ToLower();
            }
        }

        /// <summary>
        /// Wether to show the tick marks (line crossing grid) or not
        /// </summary>
        public bool IsShowTickMarks
        {
            get
            {
                if (Attributes.ContainsKey("showTickMarks"))
                    return Boolean.Parse(GetString("showTickMarks"));
                else
                    return true;
            }

            set
            {
                Attributes["showTickMarks"] = value.ToString().ToLower();
            }
        }

        /// <summary>
        /// Wether or not to show minor ticks
        /// </summary>
        public bool IsShowMirorTicks
        {
            get
            {
                if (Attributes.ContainsKey("showMirorTicks"))
                    return Boolean.Parse(GetString("showMirorTicks"));
                else
                    return true;
            }

            set
            {
                Attributes["showMirorTicks"] = value.ToString().ToLower();
            }
        }

        /// <summary>
        /// Use the color of the first series associated with this axis 
        /// for the tick marks and line bordering this axis
        /// </summary>
        public bool IsUseSeriesColor
        {
            get
            {
                if (Attributes.ContainsKey("useSeriesColor"))
                    return Boolean.Parse(GetString("useSeriesColor"));
                else
                    return true;
            }

            set
            {
                Attributes["useSeriesColor"] = value.ToString().ToLower();
            }
        }

        /// <summary>
        /// Width of line stroked at the border of the axis
        /// </summary>
        public int BorderWidth{
            get
            {
                if (Attributes.ContainsKey("borderWidth"))
                    return int.Parse(Attributes["borderWidth"]);
                else return 1;
            }
            set { Attributes["borderWidth"] = value.ToString().ToLower(); }
        }

        /// <summary>
        /// Color of the border adjacent to the axis
        /// </summary>
        public string BorderColor
        {
            get
            {
                return GetString("borderColor");
            }
            set { SetString("borderColor", value); }
        }

        /// <summary>
        /// True to try and synchronize tick spacing across multiple axes so 
        /// that ticks and grid lines line up
        /// </summary>
        public bool IsSyncTicks
        {
            get
            {
                if (Attributes.ContainsKey("syncTicks"))
                    return Boolean.Parse(GetString("syncTicks"));
                else
                    return true;
            }

            set
            {
                Attributes["syncTicks"] = value.ToString().ToLower();
            }
        }

        /// <summary>
        /// Approximate pixel spacing between ticks on graph
        /// </summary>
        public int TickSpacing
        {
            get
            {
                if (Attributes.ContainsKey("tickSpacing"))
                    return int.Parse(Attributes["tickSpacing"]);
                else return 1;
            }
            set { Attributes["tickSpacing"] = value.ToString().ToLower(); }
        }

        /// <summary>
        /// Label for the axis
        /// </summary>
        public string Label
        {
            get
            {
                return GetString("label");
            }
            set { SetString("label", value); }
        }

        /// <summary>
        /// Autoscale the axis min and max values to provide sensible tick spacing
        /// </summary>
        public bool Autoscale
        {
            get
            {
                if (Attributes.ContainsKey("autoscale"))
                    return bool.Parse(Attributes["autoscale"]);
                else return false;
            }
            set { Attributes["autoscale"] = value.ToString().ToLower(); }
        }

        protected string tickOptions {
            get
            {
                return Attributes["tickOptions"];
            }
            set{
                Attributes["tickOptions"] = value;
            }
        }        

        public Axis SetTickOptions(TickOptions options)
        {
            tickOptions = options.ToString();

            return this;
        }
    }
}
